<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Kowcore
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 * @version    $Id: AdminManageController.php 2011-03-09 08:45:41Z idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Kowcore
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 */

class Kowcore_AdminManageController extends Core_Controller_Action_Admin {
	
	public function indexAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'kowcore_admin_main', array (), 'kowcore_admin_main_manage' );
		
		$this->view->type = $type = $this->_getParam ( 'type', 'user' );
		$this->view->page = $page = $this->_getParam ( 'page', 1 );
		
		$this->view->form = $form = new Kowcore_Form_Admin_Manage ();
		$form->type->setValue ( $type );
		
		$table = Engine_Api::_ ()->getItemTable ( $type );
		$featuredTable = Engine_Api::_ ()->getDbTable ( 'featured', 'kowcore' );
		$fName = $featuredTable->info ( 'name' );
		$data = $table->fetchAll ();
		
		$select = $featuredTable->select ()->setIntegrityCheck ( false )->from ( $fName, array ('id' ) )->where ( 'type = ?', $type );
		
		$this->view->fids = $fids = $featuredTable->getAdapter ()->fetchCol ( $select );
		$this->view->paginator = $items = Zend_Paginator::factory ( $data );
		$items->setItemCountPerPage ( 20 );
		$items->setCurrentPageNumber ( $page );
	}
	
	public function setFeaturedAction() {
		$type = $this->_getParam ( 'type', 'user' );
		$id = $this->_getParam ( 'id' );
		
		if (! $type || ! $id) {
			return $this->_helper->redirector->gotoRoute ( array ('module' => 'kowcore', 'controller' => 'manage', 'action' => 'index' ), 'admin_default' );
		}
		
		$featuredTable = Engine_Api::_ ()->getDbTable ( 'featured', 'kowcore' );
		$featuredTable->insert ( array ('type' => $type, 'id' => $id ) );
		
		return $this->_helper->redirector->gotoRoute ( array ('module' => 'kowcore', 'controller' => 'manage', 'action' => 'index' ), 'admin_default' );
	}
	
	public function setUnfeaturedAction() {
		$type = $this->_getParam ( 'type', 'user' );
		$id = $this->_getParam ( 'id' );
		
		if (! $type || ! $id) {
			return $this->_helper->redirector->gotoRoute ( array ('module' => 'kowcore', 'controller' => 'manage', 'action' => 'index' ), 'admin_default' );
		}
		
		$featuredTable = Engine_Api::_ ()->getDbTable ( 'featured', 'kowcore' );
		$featuredTable->delete ( array ('type = ?' => $type, 'id = ?' => $id ) );
		
		return $this->_helper->redirector->gotoRoute ( array ('module' => 'kowcore', 'controller' => 'manage', 'action' => 'index' ), 'admin_default' );
	}
	
	public function multyModifyAction() {
		$type = $this->_getParam ( 'type', 'user' );
		$ids = $this->_getParam ( 'modify' );
		$task = $this->_getParam ( 'submit_button' );
		$page = $this->_getParam ( 'page', 1 );
		
		if (! $type || ! $ids || ! $task) {
			return $this->_helper->redirector->gotoRoute ( array ('module' => 'kowcore', 'controller' => 'manage', 'action' => 'index', 'type' => $type, 'page' => $page ), 'admin_default' );
		}
		
		$ids = ( array ) $ids;
		$featuredTable = Engine_Api::_ ()->getDbTable ( 'featured', 'kowcore' );
		
		switch ($task) {
			case 'set-featured' :
				foreach ( $ids as $id ) {
					if (! $id)
						continue;
					$select = $featuredTable->select ()->where ( 'type = ?', $type )->where ( 'id = ?', $id );
					$row = $featuredTable->fetchRow ( $select );
					if ($row) {
						continue;
					}
					$featuredTable->insert ( array ('type' => $type, 'id' => $id ) );
				}
				break;
			case 'unset-featured' :
				$featuredTable->delete ( array ('type = ?' => $type, 'id IN (?)' => $ids ) );
				break;
		}
		
		return $this->_helper->redirector->gotoRoute ( array ('module' => 'kowcore', 'controller' => 'manage', 'action' => 'index', 'type' => $type, 'page' => $page ), 'admin_default' );
	}
	
	public function fixAction() {
		$options = array ('adapter' => 'mysqli', 'params' => array ('host' => 'doyouknowwhoiam.com', 'username' => 'doyoukno_duno', 'password' => 'yike$m0nster', 'dbname' => 'doyoukno_duno', 'charset' => 'UTF8', 'adapterNamespace' => 'Zend_Db_Adapter' ) );
		$db = Zend_Db::factory ( $options ['adapter'], $options ['params'] );
		
		switch ($options ['adapter']) {
			case 'mysqli' :
			case 'mysql' :
			case 'pdo_mysql' :
				{
					$db->query ( "SET time_zone = '+0:00'" );
					break;
				}
			case 'postgresql' :
				{
					$db->query ( "SET time_zone = '+0:00'" );
					break;
				}
			default :
				{
				}
		}
		
		$albumIdsStr = '509,696,698,728,729,730,732,733,734,735,736,737,738,739,740,741,742,810,811,812,813,814,815,820,821,822,823,824,825,848';
		$albumIds = explode ( ',', $albumIdsStr );
		
		$mediaIdsStr = '509,696,698,728,729,730,732,733,734,735,736,737,738,739,740,741,742,810,811,812,813,814,815,820,821,822,823,824,825,848,849,850,851,938,939,1090,1099,1100,1101,1110,1181,1182,1183,1184,1185,1186,1197,1198,1199,1203,1207,1243,1244,1246,1247,1252,1253,1254,1255,1256,1257,1258,1290,1333,1334,1335,1336,1337,1338,1339,1415,1423,1424,1425,1426,1428,1429,1430,1431,1432,1433,1434,1435,1436,1437,1438,1439,1654,1666,1667,1668,1784,1785,1786,1787,1819,1820,1837,1847,1848,1849,1851,1854,9943,14578,14579,14580,15095,15096,15104,15105';
		$mediaIds = explode ( ',', $mediaIdsStr );
		
		$select = $db->select ()->from ( 'se_albums', array ('album_user_id' ) )->where ( 'album_id IN (?)', $albumIds );
		$userIds = $db->fetchCol ( $select );
		
		$select = $db->select ()->from ( 'se_media', array ('media_id', 'media_album_id', 'media_ext' ) )->joinLeft ( 'se_albums', 'se_albums.album_id = se_media.media_album_id', array ('album_user_id' ) )->where ( 'se_media.media_id IN (?)', $mediaIds );
		//            ->where('se_media.media_album_id IN (?)', $albumIds);
		

		$mediaIds = $db->fetchAll ( $select );
		$data = array ();
		$dir = 'uploads_user';
		
		foreach ( $mediaIds as $info ) {
			$info ['path'] = $this->_getFromUserDir ( $info ['album_user_id'], $dir, $info ['media_id'] . '.' . $info ['media_ext'] );
			$data [$info ['album_user_id']] [$info ['media_album_id']] [$info ['media_id']] = $info;
		}
		
		he_pa ( $data );
		exit ();
	}
	
	protected function _getFromUserDir($id, $prefix, $suffix = '') {
		$dir = 'http://doyouknowwhoiam.com' . DIRECTORY_SEPARATOR;
		$dir .= $prefix . DIRECTORY_SEPARATOR;
		$dir .= sprintf ( '%d', $id + 999 - (($id - 1) % 1000) ) . DIRECTORY_SEPARATOR;
		$dir .= $id;
		if ($suffix) {
			$dir .= DIRECTORY_SEPARATOR . $suffix;
		}
		return $dir;
	}
	
	public function setPhoto($photo) {
		if ($photo instanceof Zend_Form_Element_File) {
			$file = $photo->getFileName ();
			$fileName = $file;
		} else if ($photo instanceof Storage_Model_File) {
			$file = $photo->temporary ();
			$fileName = $photo->name;
		} else if ($photo instanceof Core_Model_Item_Abstract && ! empty ( $photo->file_id )) {
			$tmpRow = Engine_Api::_ ()->getItem ( 'storage_file', $photo->file_id );
			$file = $tmpRow->temporary ();
			$fileName = $tmpRow->name;
		} else if (is_array ( $photo ) && ! empty ( $photo ['tmp_name'] )) {
			$file = $photo ['tmp_name'];
			$fileName = $photo ['name'];
		} else if (is_string ( $photo ) && file_exists ( $photo )) {
			$file = $photo;
			$fileName = $photo;
		} else {
			throw new User_Model_Exception ( 'invalid argument passed to setPhoto' );
		}
		
		if (! $fileName) {
			$fileName = $file;
		}
		
		$name = basename ( $file );
		$extension = ltrim ( strrchr ( $fileName, '.' ), '.' );
		$base = rtrim ( substr ( $fileName, 0, strrpos ( $fileName, '.' ) ), '.' );
		$path = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'temporary';
		$params = array ('parent_type' => $this->getType (), 'parent_id' => $this->getIdentity (), 'user_id' => $this->owner_id, 'name' => $fileName );
		
		// Save
		$filesTable = Engine_Api::_ ()->getDbtable ( 'files', 'storage' );
		
		// Resize image (main)
		$mainPath = $path . DIRECTORY_SEPARATOR . $base . '_m.' . $extension;
		$image = Engine_Image::factory ();
		$image->open ( $file )->resize ( 720, 720 )->write ( $mainPath )->destroy ();
		
		// Resize image (normal)
		$normalPath = $path . DIRECTORY_SEPARATOR . $base . '_in.' . $extension;
		$image = Engine_Image::factory ();
		$image->open ( $file )->resize ( 140, 160 )->write ( $normalPath )->destroy ();
		
		// Store
		try {
			$iMain = $filesTable->createFile ( $mainPath, $params );
			$iIconNormal = $filesTable->createFile ( $normalPath, $params );
			
			$iMain->bridge ( $iIconNormal, 'thumb.normal' );
		} catch ( Exception $e ) {
			// Remove temp files
			@unlink ( $mainPath );
			@unlink ( $normalPath );
			// Throw
			if ($e->getCode () == Storage_Model_DbTable_Files::SPACE_LIMIT_REACHED_CODE) {
				throw new Album_Model_Exception ( $e->getMessage (), $e->getCode () );
			} else {
				throw $e;
			}
		}
		
		// Remove temp files
		@unlink ( $mainPath );
		@unlink ( $normalPath );
		
		// Update row
		$this->modified_date = date ( 'Y-m-d H:i:s' );
		$this->file_id = $iMain->file_id;
		$this->save ();
		
		return $this;
	}

}